package com.ytjj.qmyx.mall.api.h5.v1.coupon;

import com.ytjj.common.api.CommonResult;
import com.ytjj.common.constants.RedisConstants;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.model.UsersResponse;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.mall.model.response.UserCouponResponseV2;
import com.ytjj.qmyx.mall.service.ActivityService;
import com.ytjj.qmyx.mall.service.CommonService;
import com.ytjj.qmyx.users.client.UsersClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * 红包控制器
 *
 *
 * @version 1.0
 * 
 */
@RestController
@RequestMapping("/v1/coupon")
@Slf4j
public class CouponController extends BaseController {

    private final ActivityService activityService;

    private final CommonService commonService;

    private final UsersClient usersClient;

    @Resource
    private RedisService redisService;

    public CouponController(ActivityService activityService, CommonService commonService, UsersClient usersClient) {
        this.activityService = activityService;
        this.commonService = commonService;
        this.usersClient = usersClient;
    }

    /**
     * h5 用户签到领取红包
     *
     * @return
     */
    @GetMapping("/signCoupon")
    public CommonResult signCoupon() {
        UsersResponse h5CurrentUser = this.getH5CurrentUser();
        return CommonResult.success(activityService.signCoupon(h5CurrentUser));
    }

    /**
     * 优惠券列表v4,新增签到等功能 h5
     *
     * @return
     */
    @GetMapping("/getCouponListV4ByPage")
    public CommonResult getCouponListV4ByPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                              @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        UserCouponResponseV2 data = activityService.getCouponListV4ByPage(pageNum, pageSize);
        // 签到
        String token =  TokenUtil.getToken();
        String key = RedisConstants.H5_SIGN + "coupon:" + token;
        String signStr = redisService.get(key);

        // 是否签到
        if (StringUtils.isNotEmpty(signStr)) {
            data.setIsSign(1);
        }
        return CommonResult.success(data);
    }

    /**
     * 获取红包返现专区商品列表
     *
     * @return
     */
    @GetMapping("/getReturnCouponSpecial")
    public CommonResult getReturnCouponSpecial() {
        UsersResponse h5CurrentUser = this.getH5CurrentUser();
        return CommonResult.success(activityService.getReturnCouponSpecial(h5CurrentUser));
    }
}
